package com.kaifamiao.N0808;

public class Node1 {

    public static void main(String[] args) {

        String str = "pwwkew";
        int length = maxSubString(str);
        System.out.println(length);
    }

    public static int maxSubString(String str) {
        if (str == null && str.isEmpty()) {
            return -1;
        }

        if (str.length() == 1) {
            return 1;
        }

        int max = 0;

        String temp = "";
        StringBuilder subString = new StringBuilder();


        for (int i = 0; i < str.length(); i++) {
            String current = String.valueOf(str.charAt(i));
            int index = subString.indexOf(current);
            if (index >= 0) {

                if (subString.length() > max) {
                    max = subString.length();
                    temp = subString.toString();
                }

                subString.delete(0, index + 1);

                if (max >= str.length() - i + subString.length()) {
                    break;
                }
            }

            subString.append(current);
        }

        if (subString.length() > max) {
            max = subString.length();
            temp = subString.toString();
        }

        System.out.println(str + " 的最长子串是：" + temp);
        return max;
    }

}














